package com.tiza.xiaoyoubao.ui.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.flyco.pageindicator.indicator.FlycoPageIndicaor;
import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseActivity;
import com.tianze.library.utils.AppUtils;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WelcomeActivity extends BaseActivity {

    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.pageIndicaor)
    FlycoPageIndicaor flycoPageIndicaor;
    @BindView(R.id.btn_start)
    Button btn_start;
    private FragmentStatePagerAdapter adapter;
    private List<View> views = new ArrayList<>();

    private int[] images = new int[]{
            R.mipmap.welcome_1,
            R.mipmap.welcome_2,
            R.mipmap.welcome_3,
            R.mipmap.welcome_4,
            R.mipmap.welcome_5
    };

    @OnClick(R.id.btn_start)
    public void onDone(View v) {
        Hawk.put(Constants.KEY_LAST_VER, "1");
        UIHelper.showLogin(WelcomeActivity.this);
        finish();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        for (int i = 0; i < images.length; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_welcome_pager, null);
            views.add(view);
        }
        viewPager.setAdapter(new AdPagerAdapter());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(
                    int position, float positionOffset, int positionOffsetPixels
            ) {
                if (position == views.size() - 1) {
                    btn_start.setVisibility(View.VISIBLE);
                } else {
                    btn_start.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        flycoPageIndicaor.setIsSnap(true)//
                         //.setSelectAnimClass(ZoomInEnter.class)//
                         .setViewPager(viewPager, views.size());
    }

    public class AdPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            imageView.setImageResource(images[position]);
            container.addView(view);
            return views.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }
}
